﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicinaMuncii.UI.Mvc.Models.ViewModels;
using MedicinaMuncii.Entities;
using MedicinaMuncii.BL.Contracts;
using MedicinaMuncii.IoC;
using MedicinaMuncii.UI.Mvc.Properties;

namespace MedicinaMuncii.UI.Mvc.Controllers
{
    public class ApplicationController : BaseController
    {
        const string printVMSessionKey = "PrintVM";
        const string createExaminationsAction = "CreateExaminations";
        const string storeExaminationsAction = "StoreExaminations";
        ICompanyService companyService;
        IMedicalAdviceService medicalAdviceService;
        IExaminationTypeService examinationTypeService;
        IRecomandationService recomandationService;
        IExaminationService examinationService;

        #region Constructor
        public ApplicationController()
        {
            companyService = IoCWorker.Resolve<ICompanyService>();
            medicalAdviceService = IoCWorker.Resolve<IMedicalAdviceService>();
            examinationService = IoCWorker.Resolve<IExaminationService>();
            examinationTypeService = IoCWorker.Resolve<IExaminationTypeService>();
            recomandationService = IoCWorker.Resolve<IRecomandationService>();
        }
        #endregion Constructor

        public ActionResult AptitudeForm()
        {
            return View();
        }

        public ActionResult CompanyView(AptitudeFormViewModel viewModel, FormCollection form)
        {
            viewModel.Companies = companyService.GetAll().OrderBy(c => c.Name);
            viewModel.MedicalAdvices = medicalAdviceService.GetAll();
            viewModel.ExaminationTypes = examinationTypeService.GetAll();
            viewModel.Recomandations = recomandationService.GetAll();
            viewModel.Examinations = new List<Examination>();

            if (viewModel.CompanyID.HasValue)
            {
                viewModel.Company = companyService.GetByID(c => c.ID == viewModel.CompanyID.Value, new string[]
                { 
                    "address", 
                    "employees.person",
                    "employees.examinations",
                    "companyinsults.insult",
                    "employees.examinations.examinationrecomandations"
                });

                foreach (Employee employee in viewModel.Company.employees)
                {
                    foreach (Examination examination in employee.examinations
                        .Where(ex => viewModel.AllExaminations ? true : ex.Date.Date == viewModel.ExaminationsDate))
                    {
                        viewModel.Examinations.Add(examination);
                    }
                }
            }

            if (viewModel.Action == createExaminationsAction)
            {
                if (viewModel.MedicalAdviceID != null && viewModel.ExaminationTypeID != null)
                {
                    foreach (Employee employee in viewModel.Company.employees)
                    {
                        string selected = form[string.Format("Employee{0}", employee.ID)];

                        if (!string.IsNullOrEmpty(selected) && Convert.ToBoolean(selected))
                        {
                            Examination examination = new Examination();
                            examination.Date = DateTime.Now;
                            examination.EmployeeID = employee.ID;
                            examination.ExaminationTypeID = viewModel.ExaminationTypeID.GetValueOrDefault();
                            examination.MedicalAdviceID = viewModel.MedicalAdviceID.GetValueOrDefault();

                            foreach (Recomandation recomandation in viewModel.Recomandations)
                            {
                                string selectedRecomandation = form[string.Format("Recomandation{0}", recomandation.ID)];

                                if (!string.IsNullOrEmpty(selectedRecomandation))
                                {
                                    if (selectedRecomandation.IndexOf(',') != -1)
                                    {
                                        selectedRecomandation = selectedRecomandation.Substring(0, selectedRecomandation.IndexOf(','));
                                    }

                                    if (Convert.ToBoolean(selectedRecomandation))
                                    {
                                        ExaminationRecomandation examinationRecomandation = new ExaminationRecomandation();
                                        examinationRecomandation.recomandation = recomandation;
                                        examination.examinationrecomandations.Add(examinationRecomandation);
                                    }
                                }
                            }

                            examinationService.Add(examination);
                            viewModel.Examinations.Add(examination);
                        }
                    }
                }
            }

            if (viewModel.Action == storeExaminationsAction)
            {
                AptitudePrintFormViewModel printVM = new AptitudePrintFormViewModel();
                printVM.ExaminationTypes = viewModel.ExaminationTypes;
                printVM.MedicalAdvices = viewModel.MedicalAdvices;
                printVM.HostCompany = companyService.GetByID(c => c.ID == Settings.Default.HostCompanyID, new string[] { "address" });
                printVM.Examinations = new List<Examination>();

                foreach (Examination examination in viewModel.Examinations)
                {
                    string selected = form[string.Format("Examination{0}", examination.ID)];

                    if (!string.IsNullOrEmpty(selected) && Convert.ToBoolean(selected))
                    {
                        printVM.Examinations.Add(examination);
                    }
                }

                Session[printVMSessionKey] = printVM;
            }

            // Set default examination type selection
            if (viewModel.ExaminationTypeID == null && viewModel.ExaminationTypes.Count() != 0)
            {
                viewModel.ExaminationTypeID = viewModel.ExaminationTypes.First().ID;
            }

            // Set default examination type selection
            if (viewModel.MedicalAdviceID == null && viewModel.MedicalAdvices.Count() != 0)
            {
                viewModel.MedicalAdviceID = viewModel.MedicalAdvices.First().ID;
            }

            ActionResult result = null;

            switch (viewModel.Action)
            {
                case storeExaminationsAction:
                    result = new ContentResult();
                    break;

                default:
                    result = PartialView(viewModel);
                    break;
            }

            return result;
        }

        public ActionResult LoadExaminations(int companyID, bool allExaminations, int day, int month, int year)
        {
            Company company = companyService.GetByID(c => c.ID == companyID, new string[] { "address", "employees", "employees.person", "employees.examinations" });
            List<Examination> examinations = new List<Examination>();

            foreach (Employee employee in company.employees)
            {
                foreach (Examination examination in employee.examinations
                    .Where(ex => allExaminations ? true : ex.Date.Date == new DateTime(year, month, day).Date))
                {
                    examinations.Add(examination);
                }
            }

            return PartialView("Examinations", examinations);
        }

        public ActionResult PrintExaminations()
        {
            AptitudePrintFormViewModel printFormVM = null;

            if (Session[printVMSessionKey] != null)
            {
                printFormVM = (AptitudePrintFormViewModel)Session[printVMSessionKey];
                Session.Remove(printVMSessionKey);
            }

            return View("AptitudeFormPrintList", printFormVM);
        }
    }
}
